var vm = new Vue({
  el: "#app",
  data: {
    pic_src: "",
    is_c_yuan: true,//选择支付方式
    isagreement: false,//是否同意用户协议
    leixing_text: "",//类型显示
    nz_text: "",//闹钟数量 
    end_time_val: "",
    shop_list: [
      { text: '', img: [] },
      // { text: "", img: [{ id: null, src: [] },
      // { text: "请输入商品详细信息0", img: [{ id: 0, src: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1585741526787&di=da4a0be2d203c63fc8e6cdd45215c7d1&imgtype=0&src=http%3A%2F%2Fb2-q.mafengwo.net%2Fs5%2FM00%2F91%2F06%2FwKgB3FH_RVuATULaAAH7UzpKp6043.jpeg" }, { id: 2, src: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1585741526786&di=0273ac199bd30a11fc11c492bbb99a21&imgtype=0&src=http%3A%2F%2Fbbs.jooyoo.net%2Fattachment%2FMon_0905%2F24_65548_2835f8eaa933ff6.jpg" }, { id: '40', src: '/uploads/images/20200324/69b4e19044977942b6c2b3545fab9181.jpg' }] },
      // { text: "请输入商品详细信息1", img: [{ id: 0, src: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1585741526786&di=0273ac199bd30a11fc11c492bbb99a21&imgtype=0&src=http%3A%2F%2Fbbs.jooyoo.net%2Fattachment%2FMon_0905%2F24_65548_2835f8eaa933ff6.jpg" }] }
    ],
    shop_name: '',
    type: "",//活动分类
    type_diy: {},//自定义分类
    cost: 0,//活动价
    cost_type: 0,//	活动价类型
    stop_time: null,//	截止时间
    alert_time: "",//	提醒时间
    desc: null,//详情部分
    time: "",
  },
  mounted: function () {
    this.init();
  },
  watch: {
    cost: function (val) {
      this.cost = Number(val);
    },
  },
  methods: {
    init: function () {
      if ($.cookie("tiyu_leixing") && $.cookie("tiyu_leixing") != "null") {
        var leixing_cookie = JSON.parse($.cookie("tiyu_leixing"));
        this.type = leixing_cookie.id;
        // console.log(leixing_cookie.input_value[0]);
        // console.log(leixing_cookie.input_value[1]);
        // console.log(leixing_cookie.input_value[2]);
        // console.log(leixing_cookie.input_value[3]);
        // console.log(66);

        var type_diy_ = {
          a: leixing_cookie.input_value[0],
          b: leixing_cookie.input_value[1],
          c: leixing_cookie.input_value[2],
          d: leixing_cookie.input_value[3],
        }
        this.type_diy = JSON.stringify(type_diy_);
        if (leixing_cookie.input_value[3] != "") {
          this.leixing_text = leixing_cookie.input_value[3]
        } else {
          this.leixing_text = leixing_cookie.name;
        }
      }
      if ($.cookie("nz_list") && $.cookie("nz_list") != "null") {

        var nz_list = $.cookie("nz_list");
        console.log(nz_list);
        this.alert_time = nz_list;
        this.nz_text = JSON.parse(nz_list).length;
        console.log(this.nz_text);
        this.alert_time = nz_list;
      }
      if ($.cookie("activity") && $.cookie("activity") != "null") {
        console.log("获得cookie");
        var cookie_ = JSON.parse($.cookie("activity"));

        console.log(cookie_);
        this.pic = cookie_.pic;
        this.pic_src = cookie_.pic_src;
        this.is_c_yuan = cookie_.is_c_yuan;
        this.isagreement = cookie_.isagreement;
        this.shop_list = cookie_.shop_list;
        this.shop_name = cookie_.shop_name;
        // this.type = cookie_.type;
        // this.type_diy = cookie_.type_diy;
        this.cost = cookie_.cost;
        this.cost_type = cookie_.cost_type;
        this.stop_time = cookie_.stop_time;
        // this.alert_time = cookie_.alert_time;
        this.desc = cookie_.desc;
      }
    },
    //设置cookie
    set_cookie: function () {
      var cookie_ = {};
      cookie_.pic = this.pic;
      cookie_.pic_src = this.pic_src;
      cookie_.is_c_yuan = this.is_c_yuan;
      cookie_.isagreement = this.isagreement;
      cookie_.shop_list = this.shop_list;
      cookie_.shop_name = this.shop_name;
      // cookie_.type = this.type;
      // cookie_.type_diy = this.type_diy;
      cookie_.cost = this.cost;
      cookie_.cost_type = this.cost_type;
      cookie_.stop_time = this.stop_time;
      // cookie_.alert_time = this.alert_time;
      cookie_.desc = this.desc;
      $.cookie("activity", JSON.stringify(cookie_));
      console.log("logset");
    },
    //标题图片点击
    add_click: function () {
      console.log(1234563);
      // document.getElementById("add_input").click();
      console.log($("#add_input"));
      $("#add_input").click();
    },
    // 标题上传
    update_img: function (e) {
      var dom = e.currentTarget;
      console.log(dom);
      // var dom = document.getElementById('image');
      var file = dom.files[0];
      //创建formdata对象
      var formdata = new FormData();
      formdata.append("file", file);
      formdata.append("dir", "images");
      // this.pic_src = window.URL.createObjectURL(file);
      // 上传
      $.ajax({
        url: app.webUrl + "/index/plugin/execute/_plugin/WxSdk/_controller/Api/_action/file",
        type: "POST",
        data: formdata,
        processData: false,// 告诉jQuery不要去处理发送的数据
        contentType: false,
        success: function (ret) {
          var data = JSON.parse(ret);
          console.log(data);
          if (data.code == '1') {
            vm.pic_src = data.url;
            vm.pic = data.id;
            vm.set_cookie();
            // vm.send_img(data.url);
          }
        },
      });
    },


    //添加内容
    add: function () {
      this.shop_list.push({ text: '', img: [] });
    },
    //删除内容
    del_desc: function (index) {
      this.shop_list.splice(index, 1);
      vm.set_cookie();
    },
    //添加图片
    add_img: function (e, index) {
      console.log(e);
      console.log(e.currentTarget);
      var a = $(e.currentTarget).siblings("input");
      a.click();
      console.log(a);
      console.log(index);
    },
    //删除图片
    del_img: function (index,index2) {
      this.shop_list[index].img.splice(index2, 1);
          vm.set_cookie();
    },
    // 内容部分图片上传
    update_img_content: function (e, index) {
      var dom = e.currentTarget;
      console.log(dom);
      var file = dom.files[0];
      //创建formdata对象
      var formdata = new FormData();
      formdata.append("file", file);
      formdata.append("dir", "images");
      // this.pic_src = window.URL.createObjectURL(file);
      // $(e.currentTarget).append();
      // 上传
      $.ajax({
        url: app.webUrl + "/index/plugin/execute/_plugin/WxSdk/_controller/Api/_action/file",
        type: "POST",
        data: formdata,
        processData: false,// 告诉jQuery不要去处理发送的数据
        contentType: false,
        success: function (ret) {
          var data = JSON.parse(ret);
          console.log(data);
          if (data.code == '1') {
            console.log(data.id + data.url);
            var img = { id: data.id, src: data.url };
            console.log(img);

            vm.shop_list[index].img.push(img);
            vm.set_cookie();
            // vm.pid = ret.id;
            // vm.send_img(data.url);
          }
        },
      });
    },
    //改变选择积分或者人民币
    change_color: function () {
      this.is_c_yuan = !this.is_c_yuan;
      if (this.is_c_yuan == true) {
        this.cost_type = 0
      } else {
        this.cost_type = 1
      }

    },
    //缓存时间
    change_end_time: function () {

      console.log($('#date-group1-4').val());
    },
    //改变选择协议
    change_agreement: function () {
      this.isagreement = !this.isagreement;
    },
    //发布活动
    publish: function () {
      if (vm.type_diy == ""||vm.type_diy == "{}") {
        
       } else {
        vm.type_diy = JSON.parse(vm.type_diy);
        for (item in vm.type_diy) {
          if (vm.type_diy[item] == "") {
            delete vm.type_diy[item];
          }
        }
        vm.type_diy = JSON.stringify(vm.type_diy);
      }
      // setTimeout(function(){
      //   window.location.href = '/view/publish/fbhdcg.html'
      // },2000)
      if (this.shop_name == "") {
        vant.Toast({ message: "请设置活动名称");
        return;
      }
      console.log(Number(this.cost) < 0);

      if (Number(this.cost) < 0) {
        vant.Toast({ message: "请设置正确的活动价");
        return;
      }
      // if (this.cost =="") {
      //   vant.Toast({ message: "请设置正确的活动价");
      //   return;
      // }
      // this.set_cookie();
      //获取详情数据
      var list = this.shop_list.map(function (item) {
        var obj = {}
        obj.desc = item.text;
        var imgidArr = item.img.map(function (tab) {
          return tab.id
        });
        var str = imgidArr.join(',');
        obj.pics = str;
        return obj;
      });
      this.desc = JSON.stringify(list);
      console.log(list);
      //截止时间
      var date_stop = $('#date-group1-4').val();
      var stopTime = new Date(date_stop).getTime();
      var date = new Date().getTime();
      if (date_stop == "") {
        vant.Toast({ message: "请选择截止时间");
        return;
      }
      if (this.leixing_text == "") {
        vant.Toast({ message: "请选择活动类型");
        return;
      }
      if (date > stopTime) {
        vant.Toast({ message: "截止时间应该晚于当前时间");
        return;
      }
      if (this.isagreement != true) {
        vant.Toast({ message: "请同意用户协议");
        return;
      }
      this.stop_time = (stopTime / 1000);
      console.log(this.stop_time);
      for (item in vm.type_diy) {
        if (vm.type_diy.item == "") {
          vm.type_diy.item = undefined;
        }
      }
      var data = {
        name: vm.shop_name,
        pic: vm.pic,
        // type:vm.type,
        type: vm.type,
        type_diy: vm.type_diy,
        cost: vm.cost,
        cost_type: vm.cost_type,
        stop_time: vm.stop_time,
        alert_time: vm.alert_time,
        desc: vm.desc
      }
      if (vm.alert_time == "") {
        data = {
          name: vm.shop_name,
          pic: vm.pic,
          type: vm.type,
          type_diy: vm.type_diy,
          cost: vm.cost,
          cost_type: vm.cost_type,
          stop_time: vm.stop_time,
          desc: vm.desc
        }
      }
      if (vm.type_diy ==""||vm.type_diy=="{}") {
        console.log(666);
        delete data.type_diy
      }
      console.log(data);
      document.getElementById("btnButton").disabled = true;
      $.ajax({
        url: app.webUrl + "/view/api_activity/add",
        type: "POST",
        data: data,
        success: function (ret) {
          document.getElementById("btnButton").disabled = false;
          console.log(ret);
          if (ret.code == '1') {
            $.removeCookie("activity")
            $.removeCookie("tiyu_leixing")
            $.removeCookie("nz_list")
            window.localStorage.removeItem("tiyu_leixing");
            vant.Toast({ message: "活动发布成功");
            setTimeout(function () {
              window.location.href = '/view/publish/fbhdcg.html?&activityId=' + ret.data.id;
            }, 2000)
          }
        },
      });
    },
    back: function () {
      $.removeCookie("activity")
      $.removeCookie("tiyu_leixing")
      $.removeCookie("nz_list")
      window.localStorage.removeItem("tiyu_leixing");
      window.location.href = "/view/index/index.html";
    },

  },

  //
})



//
window.onload = function () {
  new Rolldate({
    el: '#date-group1-4',
    format: 'YYYY-MM-DD hh:mm',
    beginYear: 2020,
    endYear: 2100,
    theme: 'blue',
    tapBefore: function (el) {
      console.log('插件开始触发');
    },
    moveEnd: function (el, iscroll, date) {
      console.log('滚动结束');
      // console.log($('#date-group1-4').val());

    },
    confirmBefore: function (el, date) {
      console.log('确定按钮触发');
    },
    confirmEnd: function (el, date) {
      console.log('插件运行结束');
    }
  })
}


$('.set_tag .right').click(function () {
  window.location.href = '/view/publish/leixing.html'
})
$('.set_price .right').click(function () {
  $('.price_box').css('display', 'block')
  $('.price_box .cont').css('display', 'block')
})


//发布活动跳转
// $('.ljfb').click(function () {
//   window.location.href = '/view/publish/fbhdcg.html'
// })

function close_price() {
  $('.price_box').css('display', 'none')
  $('.price_box .cont').css('display', 'none')
  vm.set_cookie();
}

// 改变成本价的支付方式
// function change_color() {
//   $('.yuan').toggleClass('c_yuan')
// }

// 发布协议
// $('.hui').click(function () {
//   $('.hui').toggleClass('liang')
// })



// 点击加号，在后方生成组件
// const detail = `
// <div class="detail_cont">
//   <textarea placeholder="请编写商品详情信息..."></textarea>
//   <ul>
//     <li>
//       <img src="/public/img/chong.png" alt="">
//     </li>
//     <img src="/public/img/add.png" alt="">
//     <input type="file" multiple capture='camera' accept='image/*' style="display: none;">
//   </ul>
// </div>
// `
// $('.detail .title .add').click(function () {
//   $(detail).appendTo('.detail');
// })
